<template>
    <el-form :model="ruleForm" :rules="rules" ref="formRef" label-width="100px" class="good-form">
      <el-form-item label="用户编号" prop="id" v-if="type==='edit'">
        <el-input type="number" v-model="id" disabled></el-input>
      </el-form-item>

      <el-form-item label="姓名" prop="name">
        <el-input type="text" v-model="ruleForm.name"></el-input>
      </el-form-item>

      <el-form-item label="邮箱" prop="email">
        <el-input type="email" v-model="ruleForm.email"></el-input>
      </el-form-item>

      <el-form-item label="性别" prop="sex">
        <el-select v-model="ruleForm.sex" placeholder="选择性别">
          <el-option label="男" :value="'男'"></el-option>
          <el-option label="女"  :value="'女'"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="类别" prop="type">
        <el-select v-model="ruleForm.type" >
          <el-option label="研究员" :value="1"></el-option>
          <el-option label="领导"  :value="2"></el-option>
          <el-option label="管理员"  :value="3"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input type="number" v-model="ruleForm.password"></el-input>
      </el-form-item>
      <span class="dialog-footer">
          <el-button @click="this.$emit('close', false)">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-form>

</template>

<script>
import { ElMessage } from 'element-plus'
import http from "../utils/req";
export default {
  name: 'DialogUser',
  props: ['type', 'id'],
  data(){
    return{
      ruleForm: {
        email: '',
        password: '',
        name: '',
        sex: '',
        type: 1
      },
      rules:{
        email: [{required: true,  message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        name: [{required: true, message: '请输入姓名', trigger: 'blur'},
          {max: 10, message: '姓名长度最大为10', trigger: ['blur', 'change']}
        ],
        password: [{min: 6, max: 20, message: '密码长度在6~20之间', trigger: ['blur', 'change']} ],
      },
    }
  },
  mounted(){
    if (this.id){
      this.open(this.id)
    }
  },
  watch:{
    id(val, oldVal){
      this.reset();
      if (typeof val === 'number'){
        this.open(val)
      }
    }
  },
  methods:{
    getUserDetails(id) {
      http.get(`/user/${id}`).then(res => {
        let user = res.data.data;
        this.ruleForm = {
          name: user.name,
          email: user.email,
          sex: user.sex,
          type: user.type,
          password: ''
        }
      })
    },
    reset(){
      this.$refs['formRef'].resetFields();
    },

    open(id){
      if (id) {
        this.getUserDetails(id)
      }
    },
    submitForm (){

      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          if (this.type === 'add' ) {
            http.postDataWithSymEnc('/user/', this.ruleForm).then((res) => {
              if (res.data.code === 200){
                ElMessage.success(res.data.msg)
                this.reset()
                this.$emit('close', true)
              }else {
                ElMessage.warning(res.data.msg)
              }
            })
          } else {
            http.putDataWithSymEnc(`/user/${this.id}`, this.ruleForm).then(() => {
              ElMessage.success('修改成功')
              this.$emit('close', true)
            })
          }
        }
      })
    }
  },

}
</script>

<style scoped>
</style>